// 32feet.NET - Personal Area Networking for .NET
//
// InTheHand.Windows.Forms.SelectBluetoothDeviceDialog
// 
// Copyright (c) 2003-2008 In The Hand Ltd, All rights reserved.
// This source code is licensed under the In The Hand Community License - see License.txt

using System;
using System.ComponentModel;
using System.Net.Sockets;
using System.Windows.Forms;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Sockets;

namespace InTheHand.Windows.Forms
{
	/// <summary>
	/// Provides a form to select an available Bluetooth device.
	/// </summary>
#if WinXP
    public class SelectBluetoothDeviceDialog : CommonDialog 
    {
        private BLUETOOTH_SELECT_DEVICE_PARAMS dialog;

#else
	public class SelectBluetoothDeviceDialog : Component
    {
        private SelectBluetoothDeviceForm dialog;
#endif
	
		private BluetoothDeviceInfo device;

        /// <summary>
        /// Initializes an instance of the <see cref="SelectBluetoothDeviceDialog"/> class.
        /// </summary>
		public SelectBluetoothDeviceDialog()
		{
#if WinCE
            InTheHand.Net.PlatformVerification.ThrowException();
            dialog = new SelectBluetoothDeviceForm();
#else
            dialog = new BLUETOOTH_SELECT_DEVICE_PARAMS();
            dialog.Reset();
#endif
        }

        

#if WinCE
		/// <summary>
		/// 
		/// </summary>
		/// <returns></returns>
		public DialogResult ShowDialog()
		{
            DialogResult dr = dialog.ShowDialog();
            device = dialog.selectedDevice;
            if (dialog.fForceAuthentication)
            {
                int hresult;
                ushort handle = 0;

                    //connect to device
                    try
                    {
                        hresult = NativeMethods.BthCreateACLConnection(device.DeviceAddress.ToByteArray(), out handle);
                        if (hresult != 0)
                        {
                            //success = false;
                        }
                        else
                        {
                            //force authentication
                            hresult = NativeMethods.BthAuthenticate(device.DeviceAddress.ToByteArray());
                            /*if (hresult != 0)
                            {
                                success = false;
                            }*/
                        }

                    }
                    finally
                    {
                        if (handle != 0)
                        {
                            //close connection
                            hresult = NativeMethods.BthCloseConnection(handle);
                        }
                    }

                }
            

			return dr;
		}

        /// <summary>
        /// Resets the properties of the <see cref="SelectBluetoothDeviceDialog"/> to their default values.
        /// </summary>
        public void Reset()
        {
            dialog.Refresh();
        }
#else
        /// <summary>
        /// Specifies a common dialog box.
        /// </summary>
        /// <param name="hwndOwner">A value that represents the window handle of the owner window for the common dialog box.</param>
        /// <returns>true if the dialog box was successfully run; otherwise, false.</returns>
        protected override bool RunDialog(IntPtr hwndOwner)
        {
            //set parent window
            dialog.hwndParent = hwndOwner;

            bool success = NativeMethods.BluetoothSelectDevices(ref dialog);

            if (success)
            {
                if (dialog.cNumDevices > 0)
                {
                    device = new BluetoothDeviceInfo(dialog.Device);
                }

                bool freed = NativeMethods.BluetoothSelectDevicesFree(ref dialog);
            }

            return success;
        }

        /// <summary>
        /// Resets the properties of the <see cref="SelectBluetoothDeviceDialog"/> to their default values.
        /// </summary>
        public override void Reset()
        {
            dialog.Reset();
        }

        /// <summary>
		/// Gets or sets the information text.
		/// </summary>
		public string Info
		{
			get
			{
				return dialog.pszInfo;
			}
			set
			{
                dialog.pszInfo = value;
			}
		}

        /// <summary>
        /// If TRUE, invokes the Add New Device Wizard.
        /// </summary>
        /// <remarks>Supported only on Windows XP</remarks>
        public bool AddNewDeviceWizard
        {
            get
            {
                return dialog.fAddNewDeviceWizard;
            }
            set
            {
                dialog.fAddNewDeviceWizard = value;
            }
        }

        

        /// <summary>
        /// If TRUE, skips the Services page in the Add New Device Wizard.
        /// </summary>
        /// <remarks>Supported only on Windows XP</remarks>
        public bool SkipServicesPage
        {
            get
            {
                return dialog.fSkipServicesPage;
            }
            set
            {
                dialog.fSkipServicesPage = value;
            }
        }
#endif



        /// <summary>
		/// Gets the selected Bluetooth device.
		/// </summary>
		public BluetoothDeviceInfo SelectedDevice
		{
			get
			{
				return device;
			}
		}

        

		/// <summary>
		/// If TRUE, authenticated devices are shown in the picker.
		/// </summary>
		public bool ShowAuthenticated
		{
			get
			{
				return dialog.fShowAuthenticated;
			}
			set
			{
                dialog.fShowAuthenticated = value;
            }
		}


		/// <summary>
		/// If TRUE, remembered devices are shown in the picker.
		/// </summary>
		public bool ShowRemembered
		{
			get
			{
                return dialog.fShowRemembered;
			}
			set
			{
                dialog.fShowRemembered = value;
			}
		}

		/// <summary>
		/// If TRUE, unknown devices are shown in the picker.
		/// </summary>
		public bool ShowUnknown
		{
			get
			{
                return dialog.fShowUnknown;
			}
			set
			{
                dialog.fShowUnknown = value;
			}
		}

        /// <summary>
        /// If TRUE, forces authentication before returning.
        /// </summary>
        /// <remarks></remarks>
        public bool ForceAuthentication
        {
            get
            {
                return dialog.fForceAuthentication;
            }
            set
            {
                dialog.fForceAuthentication = value;
            }
        }
	}
}
